// This file is part of SyncStar, free software released under GPL v2.
package com.syncstar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

/** Interactive main page of the Android application. Contains buttons and menu
 * items pointing to other pages.
 */
@SuppressWarnings({"StringConcatenationInsideStringBufferAppend"})
public class WelcomeActivity extends Activity {
  /**
   * This gets called even if the screen orientation changes.
   *
   * Please note that the Activity object is destroyed when the user presses the
   * BACK button, This is documented in
   * http://developer.android.com/guide/topics/fundamentals/activities.html .
   * In this case, the state of the activity is not saved, and thus it won't
   * be restored when a new instance of the same activity class is created
   * (e.g. when the user starts the application again).
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GlobalReceiver.onActivityCreate(this);
    setContentView(R.layout.main);
    // TODO: Add a single onClick listener for multiple buttons, like in
    // http://stackoverflow.com/questions/5027147/intent-service-example-in-android
    findViewById(R.id.buttonsettings).setOnClickListener(
        new View.OnClickListener() { public void onClick(View v) {
            startActivity(new Intent(WelcomeActivity.this, ConfigActivity.class)); } });
    findViewById(R.id.buttondebug).setOnClickListener(
        new View.OnClickListener() { public void onClick(View v) {
            startActivity(new Intent(WelcomeActivity.this, DebugActivity.class)); } });
    updateWelcomeText();
  }

  @Override
  public void onResume() {
    super.onResume();
    updateWelcomeText();
  }

  void updateWelcomeText() {
    // TODO: Don't lose bolding (in strings.xml).
    // Use SpannableString etc., like
    // http://stackoverflow.com/questions/4623508/android-textview-bold-and-italic-and-underline
    ((TextView)findViewById(R.id.welcometext)).setText(
        getString(R.string.welcome).replace(
            "$(APPENGINE_APP_ROOT_URL)",
            SyncService.getRootUrlFromSyncUrl(SyncService.getSyncUrl(this))
            .toString()));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_settings) {
      Intent intent = new Intent(this, ConfigActivity.class);
      startActivity(intent);
      return true;
    }
    else if (item.getItemId() == R.id.menu_debug) {
      Intent intent = new Intent(this, DebugActivity.class);
      startActivity(intent);
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}
